home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / torus-sr.tar / torus-sr / torus / user.c < prev   
Text File  |  1991-06-19  |  5KB  |  255 lines

  1. # include "robots.h"
  2.  
  3. /*
  4.  * user.c: user oriented things
  5.  */
  6.  
  7. command()  /* whats the user trying to tell us */
  8. {
  9. retry:
  10.   if (all_moves && wimpy) good_moves();
  11.   move(my_y,my_x);
  12.   refresh();
  13.   if(last_stand) return;
  14.   bad_move = FALSE;
  15.   switch(cmd_ch=read_com()) {
  16.   case '.':
  17.   case 'h':
  18.   case 'j':
  19.   case 'k':
  20.   case 'l':
  21.   case 'y':
  22.   case 'u':
  23.   case 'b':
  24.   case 'n':
  25.   case 'w':
  26.     do_move(cmd_ch);
  27.     break;
  28.   case 't':
  29.   case 'r':
  30.   case 'T':
  31.   case 'R':
  32.   teleport:
  33.     new_x = rndx();
  34.     new_y = rndy();
  35.     move(new_y,new_x);
  36.     switch(inch()) {
  37.     case FROBOT:
  38.     case ROBOT:
  39.     case SCRAP:
  40.     case ME:
  41.       goto teleport;
  42.     }
  43.     if( (free_teleports > 0)
  44.        && ((cmd_ch == 't')||(cmd_ch =='T')) ) {
  45.       if( !isgood(new_y, new_x))
  46.         goto teleport;
  47.       free_teleports--;
  48.     }
  49.     break;
  50.   case 's':
  51.   case 'S':
  52.   case 'W':
  53.     last_stand = TRUE;
  54.     leaveok(stdscr,TRUE);
  55.     return;
  56.   case 'M':
  57.     all_moves = !all_moves;
  58.     goto retry;
  59.   case 'm':
  60.   case '?':
  61.     if (wimpy) good_moves();
  62.     else mvprintw(LINES-1,MSGPOS,"Be a stud!");
  63.     goto retry;
  64.   case 'd':
  65.   case 'D':
  66.     if(dots < 2) {
  67.       dots++;
  68.       put_dots();
  69.     } else {
  70.       erase_dots();
  71.       dots = 0;
  72.     }
  73.     goto retry;
  74.   case 'q':
  75.   case 'Q':
  76.     quit(FALSE);
  77.   case 'a':
  78.   case 'A':  /* Antimatter - sonic screwdriver */
  79.     if (free_teleports) { new_x = my_x;
  80.           new_y = my_y;
  81.           screwdriver();
  82.         }
  83.     else goto retry;
  84.     break;
  85.   case ctrl('R'):
  86.     clearok(curscr,TRUE);
  87.     wrefresh(curscr);
  88.     goto retry;
  89.   default:
  90.     bad_move = TRUE;
  91.   }
  92.   if(bad_move) {
  93.     putchar(BEL);
  94.     refresh();
  95.     count = 0;
  96.     adjacent=FALSE;
  97.     waiting=FALSE;
  98.     first_move=FALSE;
  99.     goto retry;
  100.   }
  101.   first_move = FALSE;
  102.   if(dots) erase_dots();
  103.   mvaddch(my_y,my_x,' ');
  104.   my_x = new_x;
  105.   my_y = new_y;
  106.   move(my_y,my_x);
  107.   if((inch() == ROBOT)||(inch() == FROBOT)) dead=TRUE;
  108.   else {
  109.     if(dots) put_dots();
  110.     mvaddch(my_y,my_x,ME);
  111.     refresh();
  112.   }
  113. }
  114.  
  115. read_com()
  116. {
  117.   static int     com;
  118.  
  119.   if(count == 0) {
  120.     if(isdigit(com = readchar())) {
  121.       count = com-'0';
  122.       while(isdigit(com = readchar()))
  123.         count = count*10+com-'0';
  124.     }
  125.     else { /* rfs -- eliminate possible infinite loop when running */
  126.       switch (com) {
  127.     case ctrl('W'):
  128.       /* com |= 0040; */
  129.       waiting=TRUE;
  130.     case ctrl('H'):
  131.     case ctrl('J'):
  132.     case ctrl('K'):
  133.     case ctrl('L'):
  134.     case ctrl('Y'):
  135.     case ctrl('U'):
  136.     case ctrl('B'):
  137.     case ctrl('N'):
  138.       com |= 0100;
  139.       adjacent = TRUE;
  140.       first_move=TRUE;
  141.       }
  142.       switch (com) {
  143.     case 'H':
  144.     case 'L':
  145.       count = WIDTH;
  146.       com |= 0040;
  147.       first_move=TRUE;
  148.       break;
  149.     case 'J':
  150.     case 'K':
  151.       count = HEIGHT;
  152.       com |= 0040;
  153.       first_move=TRUE;
  154.       break;
  155.     case 'Y':
  156.     case 'U':
  157.     case 'N':
  158.     case 'B':
  159.         case 'W':
  160.       { /* set count to the least common multiple of WIDTH and HEIGHT */
  161.         int w=WIDTH, h=HEIGHT,t;
  162.         while (w>h) {
  163.           t=w-h;
  164.           if (t<h) { w=h; h=t; }
  165.           else w=t;
  166.           }
  167.         count=(WIDTH/w)*HEIGHT;
  168.       }
  169.       com |= 0040;
  170.       first_move=TRUE;
  171.       break;
  172.     default:
  173.       count=0;
  174.       break;
  175.     }
  176.     }
  177.   }
  178.   if(count > 0) count--;
  179.   return(com);
  180. }
  181.  
  182. do_move(dir)  /* implement the users move */
  183.   char dir;
  184. {
  185.   register int x, y;
  186.   new_x = hbound(my_y+yinc(dir),my_x+xinc(dir));
  187.   new_y = vbound(my_y+yinc(dir),my_x+xinc(dir));
  188.   if(adjacent && !first_move) {
  189.     for(x = -2; x <= 2; x++) {
  190.       for(y = -2; y <= 2; y++) {
  191.         tmove(new_y+y ,new_x+x);
  192.         switch(inch()) {
  193.         case SCRAP:
  194.           if( waiting )
  195.             break;
  196.         case ROBOT:
  197.           if(abs(x) < 2 && abs(y) < 2) {
  198.             bad_move = TRUE;
  199.             return;
  200.           }
  201.           else break;
  202.         case FROBOT:
  203.           if (waiting &&
  204.             blocked(new_y, new_x, y, x) )
  205.             break;
  206.           bad_move = TRUE;
  207.           return;
  208.         }
  209.       }
  210.     }
  211.   }
  212.   move(new_y,new_x); /* already bounded */
  213.   switch(inch()) {
  214.   case SCRAP:
  215.     if(moveable_heaps && move_heap(dir)) break;
  216.     else {
  217.       bad_move = TRUE;
  218.       return;
  219.     }
  220.   case VERT:
  221.     if (hsew||hrev) break; 
  222.     else {
  223.       bad_move = TRUE;
  224.       return;
  225.     }
  226.   case HORIZ:
  227.     if (vsew||vrev) break;
  228.     else {
  229.       bad_move = TRUE;
  230.       return;
  231.     }
  232.   }
  233. }
  234.  
  235. move_heap(dir)  /* push a scrap heap */
  236. char  dir;
  237. {
  238.   register int  x, y;
  239.  
  240.   x = hbound(new_y + yinc(dir),new_x + xinc(dir));
  241.   y = vbound(new_y + yinc(dir),new_x + xinc(dir));
  242.   move(y, x);
  243.   switch(inch()) {
  244.     case VERT:
  245.     case HORIZ:
  246.     case SCRAP:
  247.     case ROBOT:
  248.     case FROBOT:
  249.       return FALSE;
  250.   }
  251.   addch(SCRAP);
  252.   mvaddch(new_y,new_x,' ');
  253.   return TRUE;
  254. }
  255.